<template>
  <div class="app-container">
    <div v-if="crud.props.searchToggle">
      <el-input v-model="query.name" clearable size="small" placeholder="输入名称搜索" style="width: 200px;"
                class="filter-item" @keyup.enter.native="crud.toQuery"/>
      <rrOperation/>
    </div>
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="名称(蒙)" prop="mnName">
            <el-input v-model="form.mnName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="名称(汉)" prop="zhName">
            <el-input v-model="form.zhName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="接种者(蒙)" prop="mnInoculator">
            <el-input v-model="form.mnInoculator" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="接种者(汉)" prop="zhInoculator">
            <el-input v-model="form.zhInoculator" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="接种方式(蒙)" prop="mnInoculationMethod">
            <el-input v-model="form.mnInoculationMethod" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="接种方式(汉)" prop="zhInoculationMethod">
            <el-input v-model="form.zhInoculationMethod" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="免疫期" prop="immunityPeriod">
            <el-input v-model="form.immunityPeriod" type="number" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="免疫期单位" prop="immunityPeriodUnit">
            <el-select v-model="form.immunityPeriodUnit" placeholder="请选择">
              <el-option
                v-for="item in dict.immunity_unit"
                :key="item.id"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="存储方式(蒙)" >
            <el-input v-model="form.mnStorageMethod" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="存储方式(汉)" >
            <el-input v-model="form.zhStorageMethod" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="属性(蒙)" >
            <el-input v-model="form.mnStats" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="属性(汉)" >
            <el-input v-model="form.zhStats" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="状态">
            <el-switch
              :active-value="1"
              :inactive-value="0"
              v-model="form.status"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;"
                @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="id" label="id"/>
        <el-table-column prop="mnName" label="名称(蒙)"/>
        <el-table-column prop="zhName" label="名称(汉)"/>
        <el-table-column prop="mnInoculator" label="接种者(蒙)"/>
        <el-table-column prop="zhInoculator" label="接种者(汉)"/>
        <el-table-column prop="mnInoculationMethod" label="接种方式(蒙)"/>
        <el-table-column prop="immunityPeriod" label="免疫期"/>
        <el-table-column prop="immunityPeriodUnit" label="免疫期单位">
          <template slot-scope="scope">
            {{ dict.label.immunity_unit[scope.row.immunityPeriodUnit] }}
          </template>
        </el-table-column>
        <el-table-column prop="zhStorageMethod" label="存储方式(汉)"/>
        <el-table-column prop="zhStats" label="属性(汉)"/>
        <el-table-column prop="createBy" label="创建者"/>
        <el-table-column prop="updateBy" label="更新者"/>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-switch
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              changeEnabled
              @change="changeState(scope.row,scope.row.status)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','vaccine:edit','vaccine:del'])" label="操作" width="150px"
                         align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>
  </div>
</template>

<script>
import crudVaccine from '@/api/medicine/vaccine'
import CRUD, {presenter, header, form, crud} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import {getByName} from "../../../api/medicine/vaccine";

const defaultForm = {
  id: null,
  mnName: null,
  mnNameCode: null,
  zhName: null,
  mnInoculator: null,
  mnInoculatorCode: null,
  zhInoculator: null,
  mnInoculationMethod: null,
  mnInoculationMethodCode: null,
  zhInoculationMethod: null,
  immunityPeriod: null,
  immunityPeriodUnit: null,
  mnStorageMethod: null,
  mnStorageMethodCode: null,
  zhStorageMethod: null,
  mnStats: null,
  zhStats: null,
  mnStatsCode: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null,
  status: null,
  remark: null
}
export default {
  name: 'Vaccine',
  components: {pagination, crudOperation, rrOperation, udOperation},
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['immunity_unit'],
  cruds() {
    return CRUD({title: '疫苗管理', url: 'api/vaccine', idField: 'id', sort: 'id,desc', crudMethod: {...crudVaccine}})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'vaccine:add'],
        edit: ['admin', 'vaccine:edit'],
        del: ['admin', 'vaccine:del']
      },
      rules: {
        mnName: [
          {required: true, message: '请输入名称(蒙)', trigger: 'change'}
        ],
        zhName: [
          {required: true, message: '请输入名称(汉)', trigger: 'change'}
        ],
        mnInoculator: [
          {required: true, message: '请输入接种者名称(蒙)', trigger: 'change'}
        ],
        zhInoculator: [
          {required: true, message: '请输入接种者名称(汉)', trigger: 'change'}
        ],
        mnInoculationMethod: [
          {required: true, message: '请输入接种方式(蒙)', trigger: 'change'}
        ],
        zhInoculationMethod: [
          {required: true, message: '请输入接种方式(汉)', trigger: 'change'}
        ],
        mnStorageMethod: [
          {required: true, message: '请输入存储方式(蒙)', trigger: 'change'}
        ],
        zhStorageMethod: [
          {required: true, message: '请输入存储方式(汉)', trigger: 'change'}
        ],
        mnStats: [
          {required: true, message: '请输入属性(蒙)', trigger: 'change'}
        ],
        zhStats: [
          {required: true, message: '请输入属性(汉)', trigger: 'change'}
        ],
        immunityPeriod: [
          {required: true, message: '请选择免疫期', trigger: 'change'}
        ],
        immunityPeriodUnit: [
          {required: true, message: '请选择免疫期单位', trigger: 'change'}
        ],
        input: ''
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    changeState(data, val) {
      this.$confirm('此操作将修改状态值为 ' + val + ' 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudVaccine.edit(data).then(res => {
          this.$message({
            type: 'waring',
            message: '修改成功'
          });
        });
      }).catch(() => {
        if (val === 1) {
          data.status = 0;
        } else {
          data.status = 1;
        }
        this.$message({
          type: 'info',
          message: '已取消修改'
        });
      });
    },
    search() {
      getByName(encodeURIComponent(this.input)).then(res => {
        this.crud.data = res;
      })
    }
  }
}
</script>

<style scoped>

</style>
